using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using NBusiness.CodeDom;
using NBusiness.Data.Rules;

namespace NBusiness.Data.Rules.Validation
{
    [Serializable]
    //[ExpectedArgumentType(typeof(MaxLengthEventArgs))]
    public sealed class MaxLengthRule : ValidationRule
    {
        public MaxLengthRule(Type type, string field, MaxLengthEventArgs arg)
            : base(type, field, ValidateMaxLengthRule, arg, "Resources.MaxLengthRuleMessage")
        { }

        public static bool ValidateMaxLengthRule(object value, ValidationEventArgs e)
        {
            bool retVal = true;
            if (value != null)
            {
                MaxLengthEventArgs arg = e as MaxLengthEventArgs;

                IEnumerator enumerator = (value as IEnumerable).GetEnumerator();
                int length = 0;
                while (enumerator.MoveNext()) length++;

                retVal = length <= arg.Length;
            }

            return retVal;
        }
    }
}
